package cn.lyjuan.archetypes.bms.web.controller;


import cn.lyjuan.archetypes.bms.common.entity.MgManage;
import cn.lyjuan.archetypes.bms.web.bean.vo.BaseResp;
import cn.lyjuan.archetypes.bms.web.service.RoleService;
import cn.lyjuan.archetypes.bms.web.bean.vo.PagerResp;
import cn.lyjuan.archetypes.bms.web.controller.base.SecurityController;
import cn.lyjuan.archetypes.bms.web.cst.StateConst;
import cn.lyjuan.archetypes.bms.web.service.ManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by ly on 2015/1/16.
 */
@Controller
@RequestMapping("manage/")
public class ManageController extends SecurityController
{
    @Autowired
    @Qualifier("mgManageService")
    private ManageService manageSer;

    @Autowired
    @Qualifier("mgRoleService")
    private RoleService roleSer;
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ModelAndView tolist(HttpServletRequest req)
    {
        return mv("/manage/manage_list");
    }

    @RequestMapping(value = "list", method = RequestMethod.POST)
    @ResponseBody
    public PagerResp<MgManage> list(
            @RequestParam(defaultValue = "1") Integer pn,
            Integer ps
    )
    {
        ps = null == ps ? this.getPs() : ps;
        MgManage currUser = this.getRealUser().getRealUser();

        Map<String, String> param = this.paramsToMap();

        List<MgManage> list = manageSer.queryManages(currUser, param, pn, ps);

        return BaseResp.resp(list);
    }

    @RequestMapping(value = "add", method = RequestMethod.GET)
    public ModelAndView toAdd()
    {
        return mv("/manage/manage_edit");
    }

    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public BaseResp add(MgManage manage)
    {
        MgManage creater = manageSer.queryManageByName(this.getUserName());

        manage = manageSer.addManage(creater, manage);

        return BaseResp.resp();
    }

    @RequestMapping(value = "edit", method = RequestMethod.GET)
    public ModelAndView toEdit(Integer id, Model model)
    {
        MgManage manage = manageSer.queryById(id);

        this.pkgData(model, manage);

        return mv("/manage/manage_edit");
    }

    @RequestMapping(value = "edit", method = RequestMethod.POST)
    @ResponseBody
    public BaseResp edit(MgManage toManage)
    {
        MgManage fromManage = manageSer.queryById(toManage.getId());

        manageSer.saveManage(fromManage, toManage);

        return BaseResp.resp();
    }

    @RequestMapping("status")
    @ResponseBody
    public BaseResp status(Integer id, Boolean status)
    {
        manageSer.updateStatus(id, status ? StateConst.OK : StateConst.NO);

        return BaseResp.resp();
    }
}

